<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>GetKeyState</title>
<meta name="description" content="Use GetKeyState in this free macro program to easily determine whether a keyboard key or mouse/joystick button is currently down or up.">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>GetKeyState</h1>

<p>Checks if a keyboard key or mouse/joystick button is down or up. Also retrieves joystick status.</p>

<pre class="Syntax">GetKeyState, OutputVar, KeyName [, Mode]
KeyIsDown := <a href="../Functions.htm#GetKeyState">GetKeyState</a>(&quot;KeyName&quot; [, &quot;Mode&quot;])</pre>
<h3>Parameters</h3>
<dl>

  <dt>OutputVar</dt>
  <dd><p>The name of the variable in which to store the retrieved key state, which is either D for down or U for up (but the <a href="../Functions.htm#GetKeyState">GetKeyState()</a> function returns true (1) for down and false (0) for up). The variable will be empty (blank) if the state of the key could not be determined.</p>
      <p>The items below apply only to joysticks:</p>
    <p>1) For a joystick axis such as JoyX, <em>OutputVar</em> will be set to a floating point number between 0 and 100 to indicate the joystick's position as a percentage of that axis's range of motion. The format of the number can be changed via <a href="SetFormat.htm">SetFormat</a>. This <a href="../scripts/JoystickTest.htm">test script</a> can be used to analyze your joystick(s).</p>
    <p>2) When <em>KeyName</em> is JoyPOV, the retrieved value will be between 0 and 35900. The following approximate POV values are used by many joysticks:<br>
    -1: no angle to report<br>
    0: forward POV<br>
9000 (i.e. 90 degrees): right POV <br>
27000 (i.e. 270 degrees): left POV<br>
18000 (i.e. 180 degrees): backward POV</p></dd>

  <dt>KeyName</dt>
  <dd><p>This can be just about any single character from the keyboard or one of the key names from the <a href="../KeyList.htm">key list</a>, such as a mouse/joystick button. Examples: B, 5, LWin, RControl, Alt, Enter, Escape, LButton, MButton, Joy1.</p>
      <p>Alternatively, an explicit virtual key code such as vkFF may be specified. This is useful in the rare case where a key has no name. The virtual key code of such a key can be determined by following the steps at the bottom of the <a href="../KeyList.htm#SpecialKeys">key list page</a>.</p> 
      <p><b>Known limitation:</b> This function cannot differentiate between two keys which share the same virtual key code, such as Left and NumpadLeft.</p></dd>

  <dt>Mode</dt>
  <dd><p>This parameter is ignored when retrieving joystick status.</p>
      <p>If omitted, the mode will default to that which retrieves the logical state of the key. This is the state that the OS and the active window believe the key to be in, but is not necessarily the same as the physical state.</p>
	  <p>Alternatively, one of these letters may be specified:</p>
      <p><strong>P</strong>: Retrieve the physical state (i.e. whether the user is physically holding it down). The physical state of a key or mouse button will usually be the same as the logical state unless the keyboard and/or mouse hooks are installed, in which case it will accurately reflect whether or not the user is physically holding down the key or button (as long as it was pressed down while the script was running). You can determine if your script is using the  hooks via the <a href="KeyHistory.htm">KeyHistory</a> command or menu item. You can force the hooks to be installed by adding the <a href="_InstallKeybdHook.htm">#InstallKeybdHook</a> and/or <a href="_InstallMouseHook.htm">#InstallMouseHook</a> directives to the script.</p>
      <p><strong>T</strong>: Retrieve the toggle state (only valid for  keys that can be toggled such as Capslock, Numlock, Scrolllock, and Insert). A retrieved value of D means the key is &quot;on&quot;, while U means it's &quot;off&quot; (but the <a href="../Functions.htm#GetKeyState">GetKeyState()</a> function returns true (1) for &quot;on&quot; and false (0) for &quot;off&quot;).</p>
    </dd>

</dl>

<h3>Remarks</h3>
<p>To wait for a key or mouse/joystick button to achieve a new state, it is usually easier to use <a href="KeyWait.htm">KeyWait</a> instead of a GetKeyState loop.</p>
<p>Systems with unusual keyboard drivers might be slow to update the state of their keys, especially the toggle-state of keys like Capslock. A script that checks the state of such a key immediately after it changed may use <a href="Sleep.htm">Sleep</a> beforehand to give the system time to update the key state.</p>
<p>For examples of using GetKeyState with a joystick, see the <a href="../misc/RemapJoystick.htm">joystick remapping page</a> and the <a href="../scripts/JoystickMouse.htm">Joystick-To-Mouse script</a>.</p>
<h3>Related</h3>
<p><a href="../Functions.htm#GetKeyState">GetKeyState()</a>, <a href="KeyWait.htm">KeyWait</a>, <a href="../KeyList.htm">Key List</a>, <a href="../misc/RemapJoystick.htm">Joystick remapping</a>, <a href="KeyHistory.htm">KeyHistory</a>, <a href="_InstallKeybdHook.htm">#InstallKeybdHook</a>, <a href="_InstallMouseHook.htm">#InstallMouseHook</a></p>
<h3>Examples</h3>
<pre class="NoIndent"><em>; Basic examples:</em>
GetKeyState, state, RButton  <em>; Right mouse button.</em>
GetKeyState, state, Joy2  <em>; The second button of the first joystick.</em>

GetKeyState, state, Shift
if state = D
    MsgBox At least one Shift key is down.
else
    MsgBox Neither Shift key is down.

GetKeyState, state, CapsLock, T <em>;  D if CapsLock is ON or U otherwise.</em>
state := GetKeyState(&quot;Capslock&quot;, &quot;T&quot;)  <em>; True if CapsLock is ON, false otherwise.</em>

<em>; Remapping example (this is only for illustration because it would be easier to use
; the <a href="../misc/Remap.htm">built-in remapping feature</a>):
; In the following hotkey, the mouse button is kept held down while NumpadAdd is
; down, which effectively transforms NumpadAdd into a mouse button. This method
; can also be used to repeat an action while the user is holding down a key or button:</em>
*NumpadAdd::
MouseClick, left,,, 1, 0, D  <em>; Hold down the left mouse button.</em>
Loop
{
    Sleep, 10
    GetKeyState, state, NumpadAdd, P
    if state = U  <em>; The key has been released, so break out of the loop.</em>
        break
    <em>; ... insert here any other actions you want repeated.</em>
}
MouseClick, left,,, 1, 0, U  <em>; Release the mouse button.</em>
return

<em>; Example: Make joystick button behavior depend on joystick axis position.</em>
joy2:: 
GetKeyState, joyx, JoyX 
if joyx &gt; 75 
    MsgBox Action #1 (button pressed while joystick was pushed to the right).
else if joyx &lt; 25 
    MsgBox Action #2 (button pressed while joystick was pushed to the left).
else
    MsgBox Action #3 (button pressed while joystick was centered horizontally).
return

<em>; See the <a href="../misc/RemapJoystick.htm">joystick remapping page</a> and the <a href="../scripts/JoystickMouse.htm">Joystick-To-Mouse script</a> for other examples.</em></pre>

</body>
</html>
